// import { login, logout, getInfo } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'
// import { resetRouter } from '@/router' , getUserInfo
import { getUserInfoApi, getUserDetailById } from '@/api/user'
const getDefaultState = () => {
  return {
    token: getToken() || null,
    userInfo: {}
  }
}

const state = getDefaultState()
const mutations = {
  // 设置token
  setToken(state, token) {
    state.token = token // 设置token

    setToken(token)
  },
  // 删除token
  removeToken(state) {
    state.token = null // 删除vuex的token
    setToken('')
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  // 删除用户信息
  reomveUserInfo(state) {
    state.userInfo = {}
  }
}
const actions = {
  async getuserAPI(context) {
    // console.log('a')
    const res = await getUserInfoApi()
    const ress = res.data
    const res1 = await getUserDetailById(ress.userId)
    // console.log(res1)
    // console.log('b')
    // console.log(res)

    context.commit('setUserInfo', { ...ress, ...res1.data })
    return res.data.roles.menus
  },

  logout(context) {
    // 清除token
    context.commit('removeToken')
    // 清除用户信息
    context.commit('reomveUserInfo')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions

}

